using System;
using System.Runtime;
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;


[ StructLayout( LayoutKind.Sequential , Pack = 1 ) ]
public struct SAMapPosMsg
{
	public int		MapID;
	public short	X;
	public short	Y;


};


public class SAMapPos
{
	public int mapID = 0;

	public SAMapGrid mapGrid = new SAMapGrid();
	public SAMapGrid desGrid = new SAMapGrid();
	public SAMapGrid startGrid = new SAMapGrid();
	
	public bool isMoving = false;
	public int direction = (int)SAMapDirectionType.SOUTHWEST;
	
	public float delayTime = 0.0f;

	public float speed = GameDefine.DEFAULT_MOVE_SPEED;



	public SAMapPos ()
	{

	}

	
	public void startMove( int x , int y )
	{
		if ( isMoving ) 
		{

		}

		startGrid.setValue( mapGrid.posX , mapGrid.posY );
		desGrid.setValue( x , y );

		isMoving = true;
		delayTime = 0.0f;
		
		direction = SAMapDirection.GetDirection( desGrid , startGrid );
	}

	
	public void endMove()
	{
		isMoving = false;
		mapGrid.setValue( desGrid.posX , desGrid.posY );
	}


	public void updateMove( float delay )
	{
		if ( !isMoving ) 
		{
			return;
		}
		
		delayTime += delay;
		float dis = delayTime * speed;
		
		switch ( direction ) 
		{
		case (int)SAMapDirectionType.NORTH:
		{
			if ( dis > SAMapGrid.GridInclined ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX - SAMapGrid.GridPX * dis;
			mapGrid.realPosY = startGrid.realPosY + SAMapGrid.GridPY * dis;
			mapGrid.posY = dis < SAMapGrid.GridInclinedHalf ? startGrid.posY : startGrid.posY - 1;
		}
			break;
		case (int)SAMapDirectionType.SOUTH:
		{
			if ( dis > SAMapGrid.GridInclined ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX + SAMapGrid.GridPX * dis;
			mapGrid.realPosY = startGrid.realPosY - SAMapGrid.GridPY * dis;
			mapGrid.posY = dis < SAMapGrid.GridInclinedHalf ? startGrid.posY : startGrid.posY + 1;
		}
			break;
		case (int)SAMapDirectionType.WEST:
		{
			if ( dis > SAMapGrid.GridInclined ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX - SAMapGrid.GridPX * dis;
			mapGrid.realPosY = startGrid.realPosY - SAMapGrid.GridPY * dis;
			mapGrid.posX = dis < SAMapGrid.GridInclinedHalf ? startGrid.posX : startGrid.posX - 1;

		}
			break;
		case (int)SAMapDirectionType.EAST:
		{
			if ( dis > SAMapGrid.GridInclined ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX + SAMapGrid.GridPX * dis;
			mapGrid.realPosY = startGrid.realPosY + SAMapGrid.GridPY * dis;
			mapGrid.posX = dis < SAMapGrid.GridInclinedHalf ? startGrid.posX : startGrid.posX + 1;
		}
			break;
		case (int)SAMapDirectionType.NORTHWEST:
		{
			if ( dis > SAMapGrid.GridX ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX - dis;
			mapGrid.posX = dis < SAMapGrid.GridXHalf ? startGrid.posX : startGrid.posX - 1;
			mapGrid.posY = dis < SAMapGrid.GridXHalf ? startGrid.posY : startGrid.posY - 1;
		}
			break;
		case (int)SAMapDirectionType.NORTHEAST:
		{
			if ( dis > SAMapGrid.GridY ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosY = startGrid.realPosY + dis;
			mapGrid.posX = dis < SAMapGrid.GridYHalf ? startGrid.posX : startGrid.posX + 1;
			mapGrid.posY = dis < SAMapGrid.GridYHalf ? startGrid.posY : startGrid.posY - 1;
		}
			break;
		case (int)SAMapDirectionType.SOUTHWEST:
		{
			if ( dis > SAMapGrid.GridY ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosY = startGrid.realPosY - dis;
			mapGrid.posX = dis < SAMapGrid.GridYHalf ? startGrid.posX : startGrid.posX - 1;
			mapGrid.posY = dis < SAMapGrid.GridYHalf ? startGrid.posY : startGrid.posY + 1;
		}
			break;
		case (int)SAMapDirectionType.SOUTHEAST:
		{
			if ( dis > SAMapGrid.GridX ) 
			{
				endMove();
				return;
			}
			
			mapGrid.realPosX = startGrid.realPosX + dis;
			mapGrid.posX = dis < SAMapGrid.GridXHalf ? startGrid.posX : startGrid.posX + 1;
			mapGrid.posY = dis < SAMapGrid.GridXHalf ? startGrid.posY : startGrid.posY + 1;
		}
			break;
		default:
			endMove();
			break;
		}

		
	}

}


